package no.spk.util.inforama.service.deployable.impl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import no.spk.util.inforama.service.deployable.DeployableAnalyzer;
import no.spk.util.inforama.service.pom.POM;
import no.spk.util.inforama.service.pom.ReleaseVersion;
import no.spk.util.inforama.service.pom.SnapshotVersion;
import no.spk.util.inforama.service.pom.Version;
/**
 * @author Marius
 */
public class DeployableAnalyzerImpl implements DeployableAnalyzer {

	private final String deployablePath;
	private static final String propertiesFileName = "pom.properties";
	private Properties prop;
	
	private static final String artifactId = "artifactId";
	private static final String groupId = "groupId";
	private static final String version = "version";

	public DeployableAnalyzerImpl(String deployablePath) {
		this.deployablePath = deployablePath;
	}

	public POM getPOM() {
		analyze();

		POM pom = new POM();
		pom.setArtifactId(getArtifactIdFromProperties());
		pom.setGroupId(getGroupIdFromProperties());
		pom.setVersion(createVersion(getVersionFromProperties()));
		
		return pom;
	}

	private Version createVersion(String version) {
		if(version.endsWith("-SNAPSHOT")){
			return new SnapshotVersion(version);
		}
		else {
			return new ReleaseVersion(version);
		}
	}

	private String getVersionFromProperties() {
		return prop.getProperty(version, "N/A");
		
	}

	private String getGroupIdFromProperties() {
		return prop.getProperty(groupId, "N/A");
	}

	private String getArtifactIdFromProperties() {
		return prop.getProperty(artifactId, "N/A");
	}

	private void analyze() {
		prop = new Properties();
		
		ZipInputStream zipInputStream = getZipInputStream();
		InputStream inputStreamForProperties = getInputStreamForPomProperties(zipInputStream, propertiesFileName);
		try {
			prop.load(inputStreamForProperties);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("Could not load " + propertiesFileName, e);
		}
	}

	public ZipInputStream getZipInputStream() {
		ZipInputStream zin = null;
		try {
			InputStream root = new BufferedInputStream(new FileInputStream(
					deployablePath));
			zin = new ZipInputStream(root);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return zin;
	}

	private InputStream getInputStreamForPomProperties(ZipInputStream in,
			String name) {
		ZipEntry entry;
		try {
			while ((entry = in.getNextEntry()) != null) {
				String entryName = entry.getName();

				if (entryName.endsWith(name)) {
					return in;
				}
				try {
					in.closeEntry();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
